/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __PONY_HAL_RTC_H__
#define __PONY_HAL_RTC_H__

/* includes (standard library, system) ---------------------------------------*/
#include <stdint.h>
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
/* includes (local) ----------------------------------------------------------*/
#include <hal.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* defines -------------------------------------------------------------------*/
/* typedefs ------------------------------------------------------------------*/

typedef struct hal_rtc_time {
    uint16_t year;     // 2000+
    uint8_t month;     // 1-12
    uint8_t day;       // 1-31
    uint8_t hours;     // 0-23 (24-hour format)
    uint8_t minutes;   // 0-59
    uint8_t seconds;   // 0-59
    uint8_t weekday;   // 0-6 (0=sunday)
} hal_rtc_time_t;

typedef void (*hal_rtc_alarm_callback_t)(uintptr_t arg);

typedef struct hal_rtc hal_rtc_t;

typedef struct hal_rtc_methods {
    int (*set_time)(hal_rtc_t *rtc, const hal_rtc_time_t *time);
    int (*get_time)(hal_rtc_t *rtc, hal_rtc_time_t *time);
    int (*set_alarm)(hal_rtc_t *rtc, const hal_rtc_time_t *alarm_time);
    int (*disable_alarm)(hal_rtc_t *rtc);
} hal_rtc_methods_t;

struct hal_rtc {
    hal_dev_t parent;
    hal_rtc_alarm_callback_t alarm_cb;
    uintptr_t alarm_arg;
    const hal_rtc_methods_t *methods;
};

/* macro ---------------------------------------------------------------------*/
/* inlines -------------------------------------------------------------------*/
/* externs -------------------------------------------------------------------*/

extern hal_rtc_t *hal_rtc_open(const char *name);
extern int hal_rtc_close(hal_rtc_t *rtc);
extern int hal_rtc_set_time(hal_rtc_t *rtc, const hal_rtc_time_t *time);
extern int hal_rtc_get_time(hal_rtc_t *rtc, hal_rtc_time_t *time);
extern int hal_rtc_set_alarm(hal_rtc_t *rtc, const hal_rtc_time_t *alarm_time,
                             hal_rtc_alarm_callback_t callback, uintptr_t arg);
extern int hal_rtc_disable_alarm(hal_rtc_t *rtc);
extern void hal_rtc_alarm_handler(hal_rtc_t *rtc);
extern int hal_rtc_register(hal_rtc_t *rtc, const char *name, const hal_rtc_methods_t *methods,
                            uintptr_t data);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __PONY_HAL_RTC_H__ */
